{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from gs_quant.markets import PricingContext\n",
    "from gs_quant.session import GsSession, Environment\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.instrument import IRSwap\n",
    "from gs_quant.risk import RollFwd, IRFwdRate\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# construct a portfolio of swaps with tenors 6m apart and resolve these trades\n",
    "\n",
    "tenor = ['42m', '4y', '54m', '5y']\n",
    "swap_port = [IRSwap('Pay', t, 'EUR', fixed_rate='ATM', floating_rate_frequency='6m', \n",
    "                    effective_date='6m', name='EUR6m'+t) for t in tenor]\n",
    "port = Portfolio(swap_port)\n",
    "port.resolve()\n",
    "\n",
    "fwd_results = []\n",
    "spot_results = []\n",
    "\n",
    "# calculate the fwd rate under the assumption spot rates are held constant and that fwd rates are held constant.\n",
    "# here we calculate the fwd rate every week forward in time.\n",
    "\n",
    "with PricingContext():\n",
    "    for week in range(0, 12):\n",
    "        with PricingContext(is_async=True), RollFwd(date=f'{week}w', realise_fwd=True, holiday_calendar='LDN'):\n",
    "            fwd_results.append(port.calc(IRFwdRate))\n",
    "        with PricingContext(is_async=True), RollFwd(date=f'{week}w', realise_fwd=False, holiday_calendar='LDN'):\n",
    "            spot_results.append(port.calc(IRFwdRate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# graph the results.  Note that under the fwd rate constant assumption the fwd rate doesn't change, \n",
    "# whereas you see the effect of the rolldown of the curve in the spot rate constant assumption.\n",
    "for swap in swap_port:\n",
    "    s = pd.Series([p[swap.name] for p in spot_results], dtype=np.dtype(float))\n",
    "    s.plot(figsize=(20, 12), title='Swap Carry', label='{} spot curve carry'.format(swap.name))\n",
    "    f = pd.Series([p[swap.name] for p in fwd_results], dtype=np.dtype(float))\n",
    "    f.plot(label='{} fwd curve carry'.format(swap.name))\n",
    "\n",
    "plt.xlabel('Weeks')\n",
    "plt.ylabel('Rate')\n",
    "plt.legend()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
